<?php

/**
 * @author  PVer
 * @date    2018-5-11 15:44:11
 * @version V1.0
 * @desc    
 */

namespace app\dkgzh\controller;

use cmf\controller\HomeBaseController;
use think\Validate;
use app\daikuan\model\DriverLoanModel;
use app\daikuan\model\DriverInterestModel;
use think\Db;
class JiekuanController extends HomeBaseController {

    public function ljjk() {
        $uid = cmf_get_current_user();
        if(!$uid){
            $this->error("请登录后操作",url('user/dly'));
        }
        $user = Db::name('user')->where('id',$uid['id'])->find();
        //0:未认证，1：已认证，2：认证失败,3:认证中
        if($user['dk_status'] == 0){
            $this->error('请先进行身份认证，认证通过后才可以借款',url('user/sfrz'));
        }else if($user['dk_status'] == 3){
            $this->error('后台工作人员正在审核您的个人信息，通过之后便可进行贷款',url('user/sfrz'));
        }else if($user['dk_status'] == 2){
            $this->error('身份认证未通过，请重新认证',url('user/sfrz'));
        }
        $driverInterestModel = new DriverInterestModel();
        $interest = $driverInterestModel->find();

        $this->assign("interest", $interest);
        return $this->fetch();
    }

//全部
    public function dkjd() {
        $user = cmf_get_current_user();
        $map = array();
        $map["DriverId"] = $user["id"];
        $driverLoanModel = new DriverLoanModel();
        $list = $driverLoanModel->where($map)
                ->paginate();
        $page = $list->render();
        $list= $list->toArray();
        
        //状态 0：所有未审核 ，1：审核通过，2：审核未通过，3：已放款,4:已还清,5:信息真实，6：信息不真实
        foreach($list['data'] as $key=>&$value){
            if($value['Status'] ==0){
               $value['status_txt'] = '审核中';
            }else if($value['Status'] ==1){
                $value['status_txt'] = '审核通过';
            }else if($value['Status'] ==2){
                $value['status_txt'] = '审核未通过';
            }else if($value['Status'] ==3){
                $value['status_txt'] = '已放款';
            }else if($value['Status'] ==4){
                $value['status_txt'] = '已还清';
            }else if($value['Status'] ==5){
                $value['status_txt'] = '信息已确认';
            }else if($value['Status'] ==6){
                $value['status_txt'] = '信息不真实';
            }

        }
        
        $this->assign('list', $list['data']);
        $this->assign('page', $page);
        return $this->fetch();
    }

    //审核中
    public function shz() {
        $user = cmf_get_current_user();
        $map = array();
        $map["Status"] = array('in',array('0','5'));
        $map["DriverId"] = $user["id"];
        $driverLoanModel = new DriverLoanModel();
        $list = $driverLoanModel->where($map)
                ->paginate();
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

//审核通过
    public function shtg() {
        $user = cmf_get_current_user();
        $map = array();

        $map['Status'] = ['in', array('1','3','4')];
        $map["DriverId"] = $user["id"];
        $driverLoanModel = new DriverLoanModel();
        $list = $driverLoanModel->where($map)
                ->paginate();
                
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

//审核未通过
    public function shwtg() {
        $user = cmf_get_current_user();
        $map = array();
        $map["Status"] = 2;
        $map["DriverId"] = $user["id"];
        $driverLoanModel = new DriverLoanModel();
        $list = $driverLoanModel->where($map)
                ->paginate();
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function jkPost() {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'ApplyAmount' => 'require',
                'ApplyDateLength' => 'require',
                'CargoCode' => 'require',
            ]);
            $validate->message([
                'ApplyAmount.require' => '借款金额不能为空',
                'ApplyDateLength.require' => '请输入借款时长',
                'CargoCode.require' => '货运单号不能为空',
            ]);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $userModel = new \app\user\model\UserModel;
            $log = $userModel
                    ->where("id", $data['DriverId'])
                    ->find();

            $havaLog = $log->logistics ?? 0;
            if ($log->dk_status != 1) {
                $this->error("您的个人信息未认证");
            } else
            if ($havaLog == 0) {
                $this->error("请关联物流公司后在借款");
            } else {
                $where['DriverId'] = $data['DriverId'];
                $where['Status'] = array('neq',array('2','4'));
                $loan_money = Db::name('driver_loan')->where($where)->field('sum(ApplyAmount) as total_amount')->find();
                $loan_total_money = $loan_money['total_amount']+$data['ApplyAmount'];
                $syedu = $log->can_loan_amount - $loan_money['total_amount'];
                if($syedu<0){
                    $syedu=0;
                }
                if($loan_total_money > $log->can_loan_amount){
                    $this->error("超过可以贷款的额度,还可以贷款".$syedu."元");
                }

                $arr = array();
                $arr['ApplyDate'] = $data['ApplyDate'];
                $arr['ApplyAmount'] = $data['ApplyAmount'];
                $arr['ApplyDateLength'] = $data['ApplyDateLength'];
                $arr['CargoCode'] = $data['CargoCode'];
                $arr['DriverId'] = $data['DriverId'];
                $arr['ReimAmount'] = $data['ReimAmount'];
                $arr['RepayMentDateTime'] = $data['RepayMentDateTime2'];

                $driverLoanModel = new DriverLoanModel();
                $result = $driverLoanModel->save($arr);
                $this->success("操作成功",url('dkjd'));
            }
        }
    }

}
